From f90881271f3af15c0e768b2b1b36c62f3344f4eb Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 24 Nov 2010 14:40:31 -0500 Subject: [PATCH] Add a GtkStyleContext-variant of symbolic icon loading --- gtk/gtkicontheme.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++ gtk/gtkicontheme.h | 5 +++ 2 files changed, 97 insertions(+) diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index cd55c3f140..da551e5e76 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -3071,6 +3071,16 @@ gdk_color_to_css (GdkColor *color) color->blue >> 8); } +static gchar * +gdk_rgba_to_css (GdkRGBA *color) +{ + return g_strdup_printf ("rgba(%d,%d,%d,%f)", + (gint)(color->red * 255), + (gint)(color->green * 255), + (gint)(color->blue * 255), + color->alpha); +} + static GdkPixbuf * _gtk_icon_info_load_symbolic_internal (GtkIconInfo *icon_info, const gchar *css_fg, @@ -3235,6 +3245,88 @@ gtk_icon_info_load_symbolic (GtkIconInfo *icon_info, return pixbuf; } +/** + * gtk_icon_info_load_symbolic_for_context: + * @icon_info: a #GtkIconInfo + * context: a #GtkStyleContext + * @was_symbolic: (allow-none): a #gboolean, returns whether the loaded icon + * was a symbolic one and whether the @fg color was applied to it. + * @error: (allow-none): location to store error information on failure, + * or %NULL. + * + * Loads an icon, modifying it to match the system colors for the foreground, + * success, warning and error colors provided. If the icon is not a symbolic + * one, the function will return the result from gtk_icon_info_load_icon(). + * + * This allows loading symbolic icons that will match the system theme. + * + * See gtk_icon_info_load_symbolic() for more details. + * + * Return value: (transfer full): a #GdkPixbuf representing the loaded icon + * + * Since: 3.0 + **/ +GdkPixbuf * +gtk_icon_info_load_symbolic_for_context (GtkIconInfo *icon_info, + GtkStyleContext *context, + gboolean *was_symbolic, + GError **error) +{ + GdkPixbuf *pixbuf; + GdkRGBA *color; + gchar *css_fg, *css_success; + gchar *css_warning, *css_error; + + if (!icon_info->filename || + !g_str_has_suffix (icon_info->filename, "-symbolic.svg")) + { + if (was_symbolic) + *was_symbolic = FALSE; + return gtk_icon_info_load_icon (icon_info, error); + } + + if (was_symbolic) + *was_symbolic = TRUE; + + if (gtk_style_context_lookup_color (context, "color", color)) + { + css_fg = gdk_rgba_to_css (color); + gdk_rgba_free (color); + } + + css_success = css_warning = css_error = NULL; + + if (gtk_style_context_lookup_color (context, "success_color", color)) + { + css_success = gdk_rgba_to_css (color); + gdk_rgba_free (color); + } + + if (gtk_style_context_lookup_color (context, "warning_color", color)) + { + css_warning = gdk_rgba_to_css (color); + gdk_rgba_free (color); + } + + if (gtk_style_context_lookup_color (context, "error_color", color)) + { + css_error = gdk_rgba_to_css (color); + gdk_rgba_free (color); + } + + pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info, + css_fg, css_success, + css_warning, css_error, + error); + + g_free (css_fg); + g_free (css_success); + g_free (css_warning); + g_free (css_error); + + return pixbuf; +} + /** * gtk_icon_info_load_symbolic_for_style: * @icon_info: a #GtkIconInfo diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h index f493ba98a0..07042ef35a 100644 --- a/gtk/gtkicontheme.h +++ b/gtk/gtkicontheme.h @@ -27,6 +27,7 @@ #include #include #include +#include G_BEGIN_DECLS @@ -178,6 +179,10 @@ GdkPixbuf * gtk_icon_info_load_symbolic (GtkIconInfo *icon_info GdkRGBA *error_color, gboolean *was_symbolic, GError **error); +GdkPixbuf * gtk_icon_info_load_symbolic_for_context (GtkIconInfo *icon_info, + GtkStyleContext *context, + gboolean *was_symbolic, + GError **error); GdkPixbuf * gtk_icon_info_load_symbolic_for_style (GtkIconInfo *icon_info, GtkStyle *style, GtkStateType state, -- 2.30.2